
package com.insigma.business.qrcode.common.util;

import com.insigma.business.qrcode.common.dto.BaseSearchRequest;
import com.insigma.business.qrcode.common.dto.BaseTotalBSearcher;
import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import org.apache.commons.lang3.StringUtils;


public class ResponseUtils {

    
    public static <DTO> BaseResponse<DTO> setupSuccessResponse(final BaseResponse<DTO> baseResponse, final DTO resultObject) {
        baseResponse.setSuccess(true);
        baseResponse.setDto(resultObject);

        return baseResponse;
    }

    
    public static <DTO> BasePagingResponse<DTO> setupSuccessPagingResponse(final BasePagingResponse<DTO> basePagingResponse,
                                                                           final DTO resultObject,
                                                                           final BaseSearchRequest searchRequest) {
        basePagingResponse.setPageNo(searchRequest.getPageNo());
        basePagingResponse.setPageSize(searchRequest.getPageSize());
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);

        return basePagingResponse;
    }

    
    public static <DTO> BasePagingResponse<DTO> buildSuccessPagingResponse(final BasePagingResponse<DTO> basePagingResponse,
                                                                           final DTO resultObject,
                                                                           final BaseSearchRequest searchRequest,
                                                                           final long totalCount) {
        basePagingResponse.setPageNo(searchRequest.getPageNo());
        basePagingResponse.setPageSize(searchRequest.getPageSize());
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);
        basePagingResponse.setTotalCount(totalCount);

        return basePagingResponse;
    }

    
    public static <DTO> BasePagingResponse<DTO> buildSuccessPagingResponse(final DTO resultObject,
                                                                           final BaseTotalBSearcher searcher) {
        BasePagingResponse basePagingResponse = new BasePagingResponse();
        basePagingResponse.setPageNo(searcher.getPageNo());
        basePagingResponse.setPageSize(searcher.getPageSize());
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);
        basePagingResponse.setTotalCount(searcher.getTotalCount());

        return basePagingResponse;
    }

    
    public static <DTO> BasePagingResponse<DTO> buildSuccessPagingResponse(final DTO resultObject,
                                                                           final Integer pageNo, Integer pageSize, Integer total) {
        BasePagingResponse basePagingResponse = new BasePagingResponse();
        basePagingResponse.setPageNo(pageNo);
        basePagingResponse.setPageSize(pageSize);
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);
        basePagingResponse.setTotalCount(total);

        return basePagingResponse;
    }

    public static <DTO> BasePagingResponse<DTO> buildSuccessPagingResponse(final DTO resultObject,
                                                                           final Integer pageNo, Integer pageSize, Integer total, Integer count, Double percentage) {
        BasePagingResponse basePagingResponse = new BasePagingResponse();
        basePagingResponse.setPageNo(pageNo);
        basePagingResponse.setPageSize(pageSize);
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);
        basePagingResponse.setTotalCount(total);
        basePagingResponse.setAllCount(count);
        basePagingResponse.setExtra(percentage);

        return basePagingResponse;
    }

    public static <DTO> BasePagingResponse<DTO> buildSuccessPagingResponseOfHead(final DTO resultObject,
                                                                           final Integer pageNo, Integer pageSize, Integer total, Object head) {
        BasePagingResponse basePagingResponse = new BasePagingResponse();
        basePagingResponse.setPageNo(pageNo);
        basePagingResponse.setPageSize(pageSize);
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);
        basePagingResponse.setTotalCount(total);
        basePagingResponse.setHead(head);

        return basePagingResponse;
    }

    public static <DTO> BasePagingResponse<DTO> buildSuccessPagingResponse(final DTO resultObject) {
        BasePagingResponse basePagingResponse = new BasePagingResponse();
        basePagingResponse.setPageNo(1);
        basePagingResponse.setPageSize(20);
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);
        basePagingResponse.setTotalCount(0);

        return basePagingResponse;
    }

    
    public static BaseResponse buildFailedResponse(final BaseResponse baseResponse,
                                                   final ErrorCodeEnum errorCodeEnum) {
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCodeEnum.getCode(), errorCodeEnum.getDesc()));

        return baseResponse;
    }

    
    public static BaseResponse buildFailedResponse(final BaseResponse baseResponse,
                                                   final ErrorCodeEnum errorCodeEnum,
                                                   final String customDesc) {
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCodeEnum.getCode(), StringUtils.isNotBlank(customDesc) ? customDesc : errorCodeEnum.getDesc()));

        return baseResponse;
    }

    
    public static BaseResponse buildFailedResponse(final BaseResponse baseResponse,
                                                   final ScenarioCodeEnum scenarioCodeEnum,
                                                   final ErrorCodeEnum errorCodeEnum,
                                                   final String customDesc) {
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(CommonConstant.PRODUCTION_AND_APP_CODE, scenarioCodeEnum.getCode(), errorCodeEnum.getCode(), StringUtils.isNotBlank(customDesc) ? customDesc : errorCodeEnum.getDesc()));

        return baseResponse;
    }

    public static <DTO> BaseResponse<DTO> buildSuccessResponse(BaseResponse baseResponse,DTO resultObject) {
        baseResponse.setSuccess(true);
        baseResponse.setDto(resultObject);

        return baseResponse;
    }

    public static <DTO> BaseResponse<DTO> buildSuccessResponse(DTO resultObject) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setSuccess(true);
        baseResponse.setDto(resultObject);

        return baseResponse;
    }

    public static <DTO> BaseResponse buildFailResponse(DTO resultObject) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setSuccess(false);
        baseResponse.setDto(resultObject);

        return baseResponse;
    }

    public static BaseResponse buildFailResponse(final ErrorCodeEnum errorCodeEnum) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCodeEnum.getCode(), errorCodeEnum.getDesc()));

        return baseResponse;
    }

    public static BaseResponse buildFailResponse(final ErrorCodeEnum errorCodeEnum, String desc) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCodeEnum.getCode(), desc));

        return baseResponse;
    }

    public static BaseResponse buildFailResponse(final String errorCode, String desc) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCode, desc));

        return baseResponse;
    }
}
